import axios from 'axios';
import router from "../router";
import appStore from "../store/appStore.ts";
// 创建axios实例
const request = axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_API,
    timeout: 50000, // 请求超时时间：50s
    headers: { 'Content-Type': 'application/json;charset=utf-8' },
});

const app = appStore()

request.interceptors.request.use(
    config => {
        config.headers['Authorization'] = "Bearer " + app.token
        return config;
    }
)

request.interceptors.response.use((res:any)=>{
    res.data.msg && ElMessage({type: res.data.code==200 ? "success":"error",message: res.data.msg})
    if(res.data.code==401){
        app.token = ""
        router.push('/login')
    }
    return res.data
}, (e) =>{
    ElMessage.error(e.message)
    return Promise.reject(e)
})

export default request;
